package com.lemeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.lemeng.dao.AddressMapper;
import com.lemeng.entity.PageResult;
import com.lemeng.pojo.Address;
import com.lemeng.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    AddressMapper addressMapper;

    @Override
    public List<Address> findAll() {
        return addressMapper.selectAll();
    }

    @Override
    public PageResult<Address> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        PageInfo<Address> addressPageInfo = new PageInfo<>(addressMapper.selectAll());
//        Page<Address> addresss = (Page<Address>)addressMapper.selectAll();
        return new PageResult(addressPageInfo.getTotal(),addressPageInfo.getList());
    }

    @Override
    public List<Address> findList(Map<String, Object> serchMap) {
        Example example = new Example(Address.class);
        example.setOrderByClause("is_default desc");
        Example.Criteria criteria = example.createCriteria();
        if (serchMap != null) {
            if (serchMap.get("username")!=null && StringUtil.isNotEmpty(serchMap.get("username").toString())){
                criteria.andEqualTo("username",serchMap.get("username").toString());
            }
        }
        return addressMapper.selectByExample(example);
    }

    @Override
    public PageResult<Address> findPage(Integer page, Integer size, Map<String, Object> serchMap) {
        PageHelper.startPage(page,size);
        PageInfo<Address> addressPageInfo = new PageInfo<>(findList(serchMap));
        return new PageResult(addressPageInfo.getTotal(),addressPageInfo.getList());
    }

    @Override
    public Address findById(Integer id) {
        return addressMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Address address) {
        addressMapper.insert(address);
    }

    @Override
    public void update(Address address) {
        addressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public void delete(Integer id) {
        addressMapper.deleteByPrimaryKey(id);
    }


}
